import './style';

import { Route } from 'react-router';
import { IndexRedirect, IndexRoute } from 'react-router';
import { WeaErrorPage } from 'ecCom';

import crmPortalStore from './stores';
import Home from './components/Home';
import ChangePassword from './components/ChangePassword';
import { WeaLocaleProvider } from 'ecCom';

const getLocaleLabel = WeaLocaleProvider.getLocaleLabel.bind(this, 'crm');
const getLabel = WeaLocaleProvider.getLabel;

const libs = [
  'weaWorkplan', // 日程
  'weaCrm', // 客户
  'weaDoc', // 文档
  'weaPrj', // 项目
  'weaPortal', // 门户
  'weaWorkflow', // 流程
];

const getModules = (names = []) => {
  let store = {};
  const route = [];
  names.forEach((name) => {
    const module = window[name];
    if (module && module.store && module.Route) {
      store = {
        ...store,
        ...module.store,
      };
      route.push(module.Route);
    }
  });
  return { store, route };
};

const outterResource = getModules(libs);

const stores = {
  ...crmPortalStore,
  ...outterResource.store,
};

const routes = [
  outterResource.route,
];


// window.crmPortal_store = stores;
class Error extends React.Component {
  render() {
    return (
      <WeaErrorPage ecId={`${this && this.props && this.props.ecId || ''}_WeaErrorPage@j4a0at`} msg={getLabel(129417, '服务器运行出错，请联系系统管理员！')} />
    );
  }
}

const Routes = (
  <Route ecId={`${this && this.props && this.props.ecId || ''}_Route@1pw9li`} path="main" name="main" breadcrumbName="客户门户" component={Home} onEnter={getLocaleLabel}>
    {routes}
    <Route ecId={`${this && this.props && this.props.ecId || ''}_Route@ajhpnp`} name="changePassword" path="changePassword" breadcrumbName="changePassword" component={ChangePassword} />
    <Route ecId={`${this && this.props && this.props.ecId || ''}_Route@5vxqkf`} path="*" component={Error} />
  </Route>
);

module.exports = {
  Route: Routes,
  store: stores,
};
